/*
 * @Description: 购物车模块控制器
 */
const shoppingCartCtrl = require("../model/controller/shoppingCartCtrl");
const productCtrl = require("../model/controller/productCtrl");
const checkUser = require("./utils/checkUser");

let methods = {
  /**
   * 生成购物车详细信息
   *  [{product_id,user_id,num},{...}]
   */
  async ShoppingCartData(data) {
    let shoppingCartData = [];
    for (let i = 0; i < data.length; i++) {
      const temp = data[i]; //{product_id,user_id,num}
      const product = await productCtrl.GetProductById(temp.product_id);

      let shoppingCartDataTemp = {
        id: temp.id,
        productID: temp.product_id,
        productName: product.product_name,
        productImg: product.product_picture,
        price: product.product_selling_price,
        num: temp.num,
        maxNum: Math.floor(product.product_num / 2),
        check: false,
      };

      shoppingCartData.push(shoppingCartDataTemp);
    }
    return shoppingCartData;
  },
};

module.exports = {
  /**
   * 获取购物车信息
   */
  async GetShoppingCart(req, res) {
    let { user_id } = req.body;
    // 校验用户是否登录
    // if (!checkUser(req, res)) {
    //   return;
    // }
    // 获取购物车信息 [{product_id,user_id,num},{...}]
    const shoppingCart = await shoppingCartCtrl.GetShoppingCart(user_id);
    // 生成购物车详细信息
    const data = await methods.ShoppingCartData(shoppingCart);

    let result = {
      code: "001",
      shoppingCartData: data,
    };
    res.send(result);
  },
  /**
   * 插入购物车信息
   */
  async AddShoppingCart(req, res) {
    let { user_id, product_id } = req.body;
    let result = {
      code: "005",
      msg: "添加购物车失败,未知错误",
    };
    // 校验用户是否登录
    // if (!checkUser(req, res)) {
    //   return;
    // }

    let tempShoppingCart = await shoppingCartCtrl.FindShoppingCart(
      user_id,
      product_id
    );
    //判断该用户的购物车是否存在该商品
    if (tempShoppingCart.length > 0) {
      //如果存在则把数量+1
      const tempNum = tempShoppingCart[0].num + 1;

      const product = await productCtrl.GetProductById(
        tempShoppingCart[0].product_id
      );
      const maxNum = Math.floor(product.product_num / 2);
      //判断数量是否达到限购数量
      if (tempNum > maxNum) {
        result = {
          code: "003",
          msg: "数量达到限购数量 " + maxNum,
        };
        res.send(result);
        return;
      }

      const updateData = await shoppingCartCtrl.UpdateShoppingCart(
        tempNum,
        user_id,
        product_id
      );

      if (updateData === 1) {
        result = {
          code: "002",
          msg: "该商品已在购物车，数量 +1",
        };
        return;
      }
    } else {
      //不存在则添加,新插入购物车信息
      const insertData = await shoppingCartCtrl.AddShoppingCart(
        user_id,
        product_id
      );
      // 判断是否插入成功
      if (insertData) {
        // 如果成功,获取该商品的购物车信息
        const shoppingCart = await shoppingCartCtrl.FindShoppingCart(
          user_id,
          product_id
        );
        // 生成购物车详细信息
        const data = await methods.ShoppingCartData(shoppingCart);

        result = {
          code: "001",
          msg: "添加购物车成功",
          shoppingCartData: data,
        };
        res.send(result);
        return;
      }
    }
  },
  /**
   * 删除购物车信息
   * @param {Object} ctx
   */
  async DeleteShoppingCart(req, res) {
    let { user_id, product_id } = req.body;
    // 校验用户是否登录
    // if (!checkUser(req, res)) {
    //   return;
    // }

    // 判断该用户的购物车是否存在该商品
    let tempShoppingCart = await shoppingCartCtrl.FindShoppingCart(
      user_id,
      product_id
    );

    if (tempShoppingCart.length > 0) {
      // 如果存在则删除
      const result = await shoppingCartCtrl.DeleteShoppingCart(
        user_id,
        product_id
      );
      // 判断是否删除成功
      if (result === 1) {
        let ok = {
          code: "001",
          msg: "删除购物车成功",
        };
        res.send(ok);
        return;
      }
    } else {
      // 不存在则返回信息
      let ok = {
        code: "002",
        msg: "该商品不在购物车",
      };
      res.send(ok);
    }
  },
  /**
   * 更新购物车商品数量
   */
  async UpdateShoppingCart(req,res) {
    let { user_id, product_id, num } = req.body;
    // 校验用户是否登录
    // if (!checkUser(req, res)) {
    //   return;
    // }
    // 判断数量是否小于1
    if (num < 1) {
      let ok = {
        code: "004",
        msg: "数量不合法",
      };
      res.send(ok);
      return;
    }
    // 判断该用户的购物车是否存在该商品
    let tempShoppingCart = await shoppingCartCtrl.FindShoppingCart(
      user_id,
      product_id
    );

    if (tempShoppingCart.length > 0) {
      // 如果存在则修改

      // 判断数量是否有变化
      if (tempShoppingCart[0].num == num) {
        let ok = {
          code: "003",
          msg: "数量没有发生变化",
        };
        res.send(ok);
        return;
      }
      const product = await productCtrl.GetProductById(product_id);
      const maxNum = Math.floor(product.product_num / 2);
      // 判断数量是否达到限购数量
      if (num > maxNum) {
        let ok = {
          code: "004",
          msg: "数量达到限购数量 " + maxNum,
        };
        res.send(ok);
        return;
      }

        // 修改购物车信息
        const result = await shoppingCartCtrl.UpdateShoppingCart(
          num,
          user_id,
          product_id
        );
        // 判断是否修改成功
        if (result[0] === 1) {
          let ok = {
            code: "001",
            msg: "修改购物车数量成功",
          };
          res.send(ok);
          return;
        }
    } else {
      //不存在则返回信息
      let ok = {
        code: "002",
        msg: "该商品不在购物车",
      };
      res.send(ok);
    }
  },
};
